#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
struct ListNode* Ring(int n)
{
    struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
    int i = 1;
    head->next = NULL;
    head->val = i;
    struct ListNode* tail = head;
    i = 2;
    while (i <= n)
    {
        struct ListNode* tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
        tail->next = tmp;
        tail = tail->next;
        tail->val = i;
        tail->next = NULL;
        i++;
    }
    tail->next = head;
    return tail;
}

int ysf(int n, int m) {
    if (n == 1)
        return 1;
    struct ListNode* tail = Ring(n);
    struct ListNode* pcur = tail->next;
    int count = 1;
    while (pcur != pcur->next)
    {
        if (count == m)
        {
            struct ListNode* tmp = pcur;
            pcur = pcur->next;
            tail->next = pcur;
            free(tmp);
            tmp = NULL;
            count = 1;
        }
        else {
            pcur = pcur->next;
            tail = tail->next;
            count++;
        }
    }
    return pcur->val;
}